package net.mysoft.budgetsoft.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.pagemodel.CaiDan;
import net.mysoft.budgetsoft.system.service.CaiDanService;
import net.mysoft.framework.authority.AccessType;
import net.mysoft.framework.authority.Permission;
import net.mysoft.framework.base.BaseAction;
import net.mysoft.framework.model.Json;
import net.mysoft.framework.model.SessionInfo;
import net.mysoft.framework.model.TreeNode;
import net.mysoft.framework.util.ExceptionUtil;
import net.mysoft.framework.util.ResourceUtil;
import net.mysoft.framework.util.TreeUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-12-27 08:45:08 
 */
@Action(value = "caiDanAction", results = { 
		@Result(name = "caiDan", location = "/platform/CaiDan/caiDan.jsp"), 
		@Result(name = "caiDanAdd", location = "/platform/CaiDan/caiDanAdd.jsp"), 
		@Result(name = "caiDanEdit", location = "/platform/CaiDan/caiDanEdit.jsp"), 
		@Result(name = "caiDanView", location = "/platform/CaiDan/caiDanView.jsp")})
@Controller("caiDanAction")
public class CaiDanAction extends BaseAction implements ModelDriven<CaiDan>{

	private static final Logger logger = Logger.getLogger(CaiDanAction.class);
	@Resource
	private CaiDanService caiDanService;
	
	private CaiDan caiDan = new CaiDan();
	
	public CaiDanService getCaiDanService(){
		return this.caiDanService;
	}
	
	public void setCaiDanService(CaiDanService caiDanService){
		this.caiDanService = caiDanService;
	}
	
	public CaiDan getCaiDan(){
		return this.caiDan;
	}
	
	public void setCaiDan(CaiDan caiDan){
		this.caiDan = caiDan;
	}
	
	public CaiDan getModel() {
		return caiDan;
	}
	
	@Permission(access=AccessType.LOGIN)
	public void tree() {
		try {
			List<CaiDan> list = caiDanService.find(caiDan);
			List<TreeNode> tree = new ArrayList<TreeNode>();
			for(CaiDan caiDan : list) {
				TreeNode node = new TreeNode();
				node.setId(caiDan.getBianHao());
				node.setParentId(caiDan.getShangJiCDBH() != null ? caiDan.getShangJiCDBH() : "root");
				node.setText(caiDan.getCaiDanMC());
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url", caiDan.getLuJing());
				node.setAttributes(attributes);
				tree.add(node);
			}
			super.writeJson(TreeUtil.generateTree(tree, new ArrayList(), caiDan.getRootID() != null ? caiDan.getRootID() : "root"));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
	
	
	@Permission(access=AccessType.LOGIN)
	public void meetAuthorityTree() {
		try {
			List<CaiDan> list = caiDanService.find(caiDan);
			List<TreeNode> tree = new ArrayList<TreeNode>();
			SessionInfo sessionInfo = (SessionInfo) this.getSession().getAttribute(ResourceUtil.getSessionInfoName());
			String authIds = sessionInfo.getAuthIds();
			String[] authArr = authIds.split(",");
			for(CaiDan caiDan : list){
				if(sessionInfo.getAuthIds().indexOf(ResourceUtil.ADMIN_ROLE_ID)<0&&caiDan.getBianHao().indexOf("YWZL")<0&&!sessionInfo.getUserName().equals("admin")){
					boolean flag = true;
					for (String auth : authArr) {
						if (caiDan.getBianHao().equals(auth)) {
							flag = false;
							break;
						}
					}
					if(flag) continue;
				}
				
				TreeNode node = new TreeNode();
				node.setId(caiDan.getBianHao());
				node.setParentId(caiDan.getShangJiCDBH()!= null?caiDan.getShangJiCDBH():"root");
				node.setText(caiDan.getCaiDanMC());
				//node.setState(caiDan.getState());
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url", caiDan.getLuJing());
				node.setAttributes(attributes);
				tree.add(node);
			}
			
			super.writeJson(TreeUtil.generateTree(tree,new ArrayList(),caiDan.getRootID()!=null?caiDan.getRootID():"root"));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
	
	
	/**
	 * CaiDan列表页面
	 * @return
	 */
	public String caiDan() {
		return "caiDan";
	}
	
	/**
	 * CaiDan查看页面
	 * @return
	 */
	public String caiDanView() {
		return "caiDanView";
	}
	
	/**
	 * CaiDan添加页面
	 * @return
	 */
	public String caiDanAdd() {
		return "caiDanAdd";
	}
	
	/**
	 * CaiDan编辑页面
	 * @return
	 */
	public String caiDanEdit() {
		return "caiDanEdit";
	}
	
	/**
	 * 添加
	 */
	public void add() {
		Json j = new Json();
		try {
			caiDanService.add(caiDan);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 修改
	 */
	public void edit() {
		Json j = new Json();
		try {
			caiDanService.edit(caiDan);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 删除
	 */
	public void delete() {
		Json j = new Json();
		try {
			caiDanService.delete(caiDan);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 列表
	 */
	public void datagrid() {
		try {
			super.writeJson(caiDanService.datagrid(caiDan));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
}
